﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
using SubSonic;


/// <summary>
/// Summary description for TemplateHandler
/// </summary>


namespace Business
{

    public class TemplateHandler : ITemplate
    {
        public string Advisor { get; set; }

        public string Employer { get; set; }

        public string FourthFactor { get; set; }


        public string Timing { get; set; }


        public string BonyadeShahrestan { get; set; }

        private bool _HasRepeat;
        public bool HasRepeat
        {
            get { return _HasRepeat; }
            set
            {
                _HasRepeat = value;
            }
        }

        private int _RepeatCount;
        public int RepeatCount
        {
            get { return _RepeatCount; }
            set
            {
                _RepeatCount = value;
            }
        }

        private Guid _ContractId;
        public Guid ContractId
        {
            get { return _ContractId; }
            set
            {
                _ContractId = value;
            }
        }
        private bool _IsSingleRowOutput;
        public bool IsSingleRowOutput
        {
            get { return _IsSingleRowOutput; }
            set
            {
                _IsSingleRowOutput = value;
            }
        }
        

        private Guid _StepId;
        public Guid StepId
        {
            get { return _StepId; }
            set
            {
                _StepId = value;
            }
        }

        private ProjectStatusTaskType _ProjectStatus;
        public ProjectStatusTaskType ProjectStatus
        {
            get { return _ProjectStatus; }
            set { _ProjectStatus = value; }
        }



        void ITemplate.InstantiateIn(Control container)
        {
            //if (!HasRepeat)
            //{
            ///for normal task columns
            LiteralControl literalControl = new LiteralControl();
            if (!IsSingleRowOutput)
            {
                literalControl.Text =
                    "<table style=\"width: 100px; height: 50px\"> <tr> <td>" + Employer + "</td> </tr> <tr> <td> " + Advisor + "</td> </tr> <tr>  <td>"
                    + FourthFactor + " </td> </tr> <tr> <td>"
                    + BonyadeShahrestan +
                    "</td> </tr> <tr> <td>"
                    + Timing + " </td> </tr> </table>";
                
            }
            else
            {
                literalControl.Text =
            "<table style=\"width: 100px; height: 50px\"> <tr> <td>" + Employer +  Advisor + FourthFactor + BonyadeShahrestan + "</td> </tr> <tr> <td>" + Timing + " </td> </tr> </table>";
            }

            container.Controls.Add(literalControl);

            //}
            //else
            //{
            //    ///For repeated task columns
            //    Repeater repeater = new Repeater();
            //    Query q = new Query(TblTask.Schema);
            //    TblTaskCollection tasks = new TblTaskCollection();
            //    List<RepeatTaskType> repeatTasks = new List< RepeatTaskType >();
            //    if (CommonCalculations.ContractType(ProjectStatus.ContractId))
            //    {
            //        //Contract Is for Advisors
            //        q.AddWhere(TblTask.Columns.AdvisorStepId, ProjectStatus.AdvisorStepId);
            //        tasks.LoadAndCloseReader( TblTask.FetchByQuery(q) );
            //    }
            //    else
            //    {
            //        //Contract is for survyours
            //        q.AddWhere(TblTask.Columns.SurveyorStepId, ProjectStatus.SurveyorStepId);
            //        tasks.LoadAndCloseReader(TblTask.FetchByQuery(q));
            //    }
            //    foreach ( var task in tasks )
            //    {
            //        TblTask thisTask =  TblTask.FetchByID(task.Id);
            //        Advisor = Employer = Timing = BonyadeShahrestan = FourthFactor = "--";
            //        switch (ProjectStatus.InputGuy)
            //        {
            //            case Business.InputGuyType.InputGuy.Advisor:
            //                Advisor = ProjectStatus.DateDoneFarsi;
            //                break;
            //            case Business.InputGuyType.InputGuy.BonyadeShahrestan:
            //                BonyadeShahrestan = ProjectStatus.DateDoneFarsi;
            //                break;
            //            case Business.InputGuyType.InputGuy.Employer:
            //                Employer = ProjectStatus.DateDoneFarsi;
            //                break;
            //            case Business.InputGuyType.InputGuy.FourthFactor:
            //                FourthFactor = ProjectStatus.DateDoneFarsi;
            //                break;
            //            default:
            //                Advisor = Employer = Timing = BonyadeShahrestan = FourthFactor = "--";
            //                break;
            //        }
            //        RepeatTaskType repeatTask = new RepeatTaskType(Advisor, Employer, FourthFactor, BonyadeShahrestan, ProjectStatus.Latency);
            //        repeatTasks.Add( repeatTask );
            //    }
            //    repeater.DataSource = repeatTasks;
            //}
        }

        public TemplateHandler(string advisor, string employer, string bonyadeshahrestan, string fourthFactor, string timing)
        {
            Advisor = advisor;
            Employer = employer;
            FourthFactor = fourthFactor;
            Timing = timing;
            BonyadeShahrestan = bonyadeshahrestan;
            IsSingleRowOutput = false;
        }


        public TemplateHandler(string advisor, string employer, string bonyadeshahrestan, string fourthFactor, string timing, bool isSingleRowOutput)
        {
            Advisor = advisor;
            Employer = employer;
            FourthFactor = fourthFactor;
            Timing = timing;
            BonyadeShahrestan = bonyadeshahrestan;
            IsSingleRowOutput = isSingleRowOutput;
        }

        public TemplateHandler(ProjectStatusTaskType projectStatus)
        {
            ProjectStatus = projectStatus;
            HasRepeat = projectStatus.HasRepeat;
            RepeatCount = projectStatus.RepeatCount;
        }

        public TemplateHandler()
        {
        }

    }
}
